<template>
  <div v-if="quantity" class="price-display">
    <div class="price-summary">
      <div class="total-price">
        <span class="label">总价：</span>
        <span class="value">¥{{ priceData.totalPrice }}</span>
      </div>
      <div class="unit-price">
        <span class="label">{{ isRollFilm ? "单价：" : "单价：" }}</span>
        <span class="value">¥{{ priceData.unitPrice }}/{{ isRollFilm ? "kg" : "个" }}</span>
      </div>
      <div v-if="isRollFilm" class="weight-display">
        <span class="label">重量：</span>
        <span class="value">{{ quantity }}kg</span>
      </div>
    </div>

    <div v-if="showDetails" class="price-details">
      <el-collapse v-model="activeCollapse">
        <el-collapse-item :title="isRollFilm ? '卷膜计算明细' : '计算明细'" name="details">
          <div class="calculation-details">
            <!-- 卷膜模式的明细 -->
            <div v-if="isRollFilm" class="detail-section">
              <h4>卷膜信息</h4>
              <div class="detail-item">
                <span>重量：{{ priceData.details.weight || quantity }} kg</span>
              </div>
              <div v-if="priceData.details.rollMaterial" class="detail-item">
                <span>材质：{{ priceData.details.rollMaterial }}</span>
              </div>
              <div v-if="priceData.details.rollType" class="detail-item">
                <span>卷膜规格：{{ priceData.details.rollType }}</span>
              </div>
              <div
                v-if="
                  priceData.details.rollCompositeType &&
                  priceData.details.rollCompositeType.length > 0
                "
                class="detail-item"
              >
                <span>复合工艺：{{ priceData.details.rollCompositeType.join(", ") }}</span>
              </div>
              <div class="detail-item">
                <span>基础单价：¥{{ priceData.details.basePrice || 0 }}/kg</span>
              </div>
              <div v-if="priceData.details.compositePrice > 0" class="detail-item">
                <span>复合工艺单价：¥{{ priceData.details.compositePrice || 0 }}/kg</span>
              </div>
              <div class="detail-item">
                <span>实际单价：¥{{ priceData.unitPrice }}/kg</span>
              </div>
              <div v-if="priceData.details.boxFee > 0" class="detail-item">
                <span>
                  纸箱费用：{{ priceData.details.boxCount }}个纸箱 × ¥{{
                    priceData.details.boxPrice || 3.5
                  }}/个 = ¥{{ priceData.details.boxFee }}
                  (15kg/箱)
                </span>
              </div>
              <div class="detail-item">
                <span>
                  总价：{{ priceData.details.weight || quantity }}kg × ¥{{ priceData.unitPrice }}/kg
                  <span v-if="priceData.details.boxFee > 0">
                    + ¥{{ priceData.details.boxFee }}(纸箱)
                  </span>
                  = ¥{{ priceData.totalPrice }}
                </span>
              </div>
            </div>

            <!-- 非卷膜模式的明细（原有逻辑） -->
            <template v-else>
              <!-- 基础信息 -->
              <div class="detail-section">
                <h4>基础信息</h4>
                <div class="detail-item">
                  <span>袋子面积：{{ priceData.details.area }} m²</span>
                </div>
                <div class="weight-breakdown">
                  <div class="detail-item">
                    <span>原材料重量：{{ priceData.details.materialWeight || 0 }} kg</span>
                  </div>
                  <div class="detail-item">
                    <span>配件重量：{{ priceData.details.accessoryWeight || 0 }} kg</span>
                  </div>
                  <div class="detail-item total-weight">
                    <span>
                      总重量：{{ priceData.details.totalWeight || priceData.details.weight || 0 }}
                      kg
                    </span>
                  </div>
                </div>
              </div>
              <div v-hasPerm="['vt:vt-order-form:price-detail']">
                <!-- 材质费用 -->
                <div v-if="priceData.details.material.price > 0" class="detail-section">
                  <h4>材质费用：¥{{ priceData.details.material.price }}</h4>
                  <div
                    v-for="item in priceData.details.material.details"
                    :key="item.name"
                    class="detail-item"
                  >
                    <span>
                      {{ item.name }}({{ item.thickness }}mm)：{{ item.weight }}kg × ¥{{
                        item.unitPrice
                      }}/kg = ¥{{ item.price }}
                    </span>
                  </div>
                </div>

                <!-- 制袋工艺费用 -->
                <div v-if="priceData.details.bagProcess.price > 0" class="detail-section">
                  <h4>制袋工艺费用：¥{{ priceData.details.bagProcess.price }}</h4>
                  <div
                    v-for="item in priceData.details.bagProcess.details"
                    :key="item.name"
                    class="detail-item"
                  >
                    <span>
                      {{ item.name }}：{{ item.quantity }}个 × ¥{{ item.unitPrice }}/个 = ¥{{
                        item.price
                      }}
                    </span>
                  </div>
                </div>

                <!-- 复合工艺费用 -->
                <div v-if="priceData.details.composite.price > 0" class="detail-section">
                  <h4>复合工艺费用：¥{{ priceData.details.composite.price }}</h4>
                  <div
                    v-for="item in priceData.details.composite.details"
                    :key="item.name"
                    class="detail-item"
                  >
                    <div class="surface-detail">
                      <div class="surface-name">{{ item.name }}：</div>
                      <div class="sub-detail">
                        按面积计算：{{ item.area }}m² × ¥{{ item.unitPrice }}/m² = ¥{{
                          item.areaPriceCalc
                        }}
                      </div>
                      <div v-if="item.minFee > 0" class="sub-detail">
                        最低价格：¥{{ item.minFee }}
                      </div>
                      <div class="sub-detail final-price">
                        实际价格：¥{{ item.price }}
                        <span v-if="item.usedMinFee" class="min-fee-tag">(使用最低价格)</span>
                      </div>
                    </div>
                  </div>
                </div>

                <!-- 印刷颜色费用 -->
                <div v-if="priceData.details.surface.price > 0" class="detail-section">
                  <h4>印刷颜色费用：¥{{ priceData.details.surface.price }}</h4>
                  <div
                    v-for="item in priceData.details.surface.details"
                    :key="item.name"
                    class="detail-item"
                  >
                    <div class="surface-detail">
                      <div class="surface-name">{{ item.name }}：</div>
                      <div class="sub-detail">
                        按面积计算：{{ item.area }}m² × ¥{{ item.unitPrice }}/m² = ¥{{
                          item.areaPriceCalc
                        }}
                      </div>
                      <div v-if="item.minFee > 0" class="sub-detail">
                        最低价格：¥{{ item.minFee }}
                      </div>
                      <div class="sub-detail final-price">
                        实际价格：¥{{ item.price }}
                        <span v-if="item.usedMinFee" class="min-fee-tag">(使用最低价格)</span>
                      </div>
                    </div>
                  </div>
                </div>

                <!-- 配件费用 -->
                <div v-if="priceData.details.accessory.price > 0" class="detail-section">
                  <h4>
                    配件费用：¥{{ priceData.details.accessory.price }}
                    <span v-if="priceData.details.accessory.weight > 0" class="accessory-weight">
                      (重量：{{ priceData.details.accessory.weight }}kg)
                    </span>
                  </h4>
                  <div
                    v-for="item in priceData.details.accessory.details"
                    :key="item.name"
                    class="detail-item"
                  >
                    <div class="accessory-detail">
                      <div class="accessory-name">{{ item.name }}：</div>
                      <div class="sub-detail">
                        用量：{{ item.length || 0 }}{{ item.unit }} × {{ item.quantity }}个 =
                        {{ item.sumLength || 0 }}{{ item.unit }}
                      </div>
                      <div class="sub-detail">
                        价格：{{ item.sumLength || 0 }}{{ item.unit }} × ¥{{ item.unitPrice }}/{{
                          item.unit
                        }}
                        = ¥{{ item.price }}
                      </div>
                      <div v-if="item.unitWeight > 0" class="sub-detail">
                        重量：{{ item.sumLength || 0 }}{{ item.unit }} × {{ item.unitWeight }}kg/{{
                          item.unit
                        }}
                        = {{ item.weight || 0 }}kg
                      </div>
                    </div>
                  </div>
                </div>
                <!-- 额外损耗费用 -->
                <div
                  v-if="priceData.details.extraLossFee > 0"
                  class="detail-section extra-loss-section"
                >
                  <h4>额外损耗费用：¥{{ priceData.details.extraLossFee }}</h4>
                  <div class="detail-item">
                    <span>
                      基础总价：¥{{ priceData.details.baseTotalPrice }} ×
                      {{ priceData.details.extraLossRate * 100 }}% = ¥{{
                        priceData.details.extraLossFee
                      }}
                      <span v-if="priceData.details.hasAluminumFoil" class="aluminum-foil-tag">
                        (含复合铝箔)
                      </span>
                    </span>
                  </div>
                </div>

                <!-- 价格损耗费用 -->
                <div
                  v-if="priceData.details.priceLossFee > 0"
                  class="detail-section price-loss-section"
                >
                  <h4>小袋子损耗：¥{{ priceData.details.priceLossFee }}</h4>
                  <div class="detail-item">
                    <span>未加损耗前单价：¥{{ priceData.details.unitPriceBeforeLoss }}/个</span>
                  </div>
                  <div class="detail-item">
                    <span>
                      基础总价：¥{{ priceData.details.baseTotalPrice }} ×
                      {{ priceData.details.priceLossRate }}% = ¥{{ priceData.details.priceLossFee }}
                    </span>
                  </div>
                </div>
              </div>

              <!-- 版费 -->
              <!-- <div v-if="priceData.details.plateFee > 0" class="detail-section">
                <h4>版费：¥{{ priceData.details.plateFee }}</h4>
              </div> -->

              <!-- 纸箱费用 -->
              <div v-if="priceData.details.boxFee > 0" class="detail-section">
                <h4>纸箱费用：¥{{ priceData.details.boxFee }}</h4>
                <div class="detail-item">
                  <span>
                    {{ priceData.details.boxCount }}个纸箱 × ¥{{
                      priceData.details.boxPrice || 3.5
                    }}/个 = ¥{{ priceData.details.boxFee }}
                    (15kg/箱)
                  </span>
                </div>
              </div>

              <!-- 合计 -->
              <div class="detail-section total-section">
                <h4>合计：¥{{ priceData.totalPrice }}</h4>
                <div class="detail-item">
                  <span>
                    单价：¥{{ priceData.totalPrice }} ÷ {{ quantity }} = ¥{{
                      priceData.unitPrice
                    }}/个
                  </span>
                </div>
                <div v-if="priceData.details.totalWeight > 0" class="detail-item">
                  <span>
                    总重量：{{ priceData.details.totalWeight }}kg (原材料：{{
                      priceData.details.materialWeight || 0
                    }}kg + 配件：{{ priceData.details.accessoryWeight || 0 }}kg)
                  </span>
                </div>
              </div>
            </template>
          </div>
        </el-collapse-item>
      </el-collapse>
    </div>
  </div>
</template>

<script setup>
import { ref } from "vue";

defineOptions({
  name: "PriceDisplay",
  inheritAttrs: false,
});

defineProps({
  priceData: {
    type: Object,
    default: () => ({
      totalPrice: 0,
      unitPrice: 0,
      details: {
        area: 0,
        materialWeight: 0,
        accessoryWeight: 0,
        totalWeight: 0,
        weight: 0,
        rollType: "",
        basePrice: 0,
        rollMaterial: "",
        rollCompositeType: [],
        compositePrice: 0,
        material: { price: 0, details: [] },
        bagProcess: { price: 0, details: [] },
        composite: { price: 0, details: [] },
        surface: { price: 0, details: [] },
        accessory: { price: 0, weight: 0, details: [] },
        plateFee: 0,
        boxFee: 0,
        boxCount: 0,
        boxPrice: 3.5,
        extraLossFee: 0,
        extraLossRate: 0,
        baseTotalPrice: 0,
        hasAluminumFoil: false,
        unitPriceBeforeLoss: 0,
        priceLossRate: 0,
        priceLossFee: 0,
      },
    }),
  },
  quantity: {
    type: Number,
    default: 0,
  },
  showDetails: {
    type: Boolean,
    default: true,
  },
  isRollFilm: {
    type: Boolean,
    default: false,
  },
});

const activeCollapse = ref(["details"]);
</script>

<style scoped lang="scss">
.price-display {
  border: 1px solid #e4e7ed;
  border-radius: 4px;
  padding: 12px;
  background-color: #f8f9fa;
  width: 100%;

  .price-summary {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
    padding: 8px 12px;
    background-color: #fff;
    border-radius: 4px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);

    .total-price,
    .unit-price,
    .weight-display {
      display: flex;
      align-items: center;

      .label {
        font-weight: 500;
        color: #606266;
        margin-right: 8px;
      }

      .value {
        font-size: 16px;
        font-weight: 600;
        color: #e6a23c;
      }
    }
  }

  .price-details {
    .calculation-details {
      .detail-section {
        margin-bottom: 16px;
        padding: 12px;
        background-color: #fff;
        border-radius: 4px;
        border-left: 3px solid #409eff;

        h4 {
          margin: 0 0 8px 0;
          font-size: 14px;
          font-weight: 600;
          color: #303133;
        }

        .detail-item {
          margin-bottom: 4px;
          font-size: 13px;
          color: #606266;
          line-height: 1.4;

          &:last-child {
            margin-bottom: 0;
          }

          &.total-weight {
            font-weight: 500;
            color: #303133;
            padding: 4px 0;
            border-top: 1px solid #f0f0f0;
            margin-top: 4px;
          }

          .surface-detail {
            .surface-name {
              font-weight: 500;
              margin-bottom: 2px;
            }

            .sub-detail {
              margin-left: 12px;
              margin-bottom: 2px;
              font-size: 12px;

              &.final-price {
                font-weight: 500;
                color: #303133;
              }

              .min-fee-tag {
                color: #e6a23c;
                font-size: 11px;
                font-weight: normal;
              }

              .aluminum-foil-tag {
                color: #f56c6c;
                font-size: 11px;
                font-weight: normal;
                background-color: #fef0f0;
                padding: 1px 4px;
                border-radius: 2px;
                margin-left: 4px;
              }
            }
          }

          .accessory-detail {
            .accessory-name {
              font-weight: 500;
              margin-bottom: 2px;
            }

            .sub-detail {
              margin-left: 12px;
              margin-bottom: 2px;
              font-size: 12px;
              color: #909399;
            }
          }
        }

        h4 {
          .accessory-weight {
            font-size: 12px;
            font-weight: normal;
            color: #909399;
            margin-left: 8px;
          }
        }

        .weight-breakdown {
          background-color: #fafafa;
          border-radius: 4px;
          padding: 8px;
          margin-top: 8px;
          border-left: 2px solid #67c23a;
        }

        &.total-section {
          border-left-color: #67c23a;
          background-color: #f0f9ff;

          h4 {
            color: #67c23a;
            font-size: 16px;
          }
        }

        &.extra-loss-section {
          border-left-color: #f56c6c;
          background-color: #fef0f0;

          h4 {
            color: #f56c6c;
            font-size: 16px;
          }
        }

        &.price-loss-section {
          border-left-color: #e6a23c;
          background-color: #fdf6ec;

          h4 {
            color: #e6a23c;
            font-size: 16px;
          }
        }
      }
    }

    :deep(.el-collapse) {
      border: none;

      .el-collapse-item__header {
        background-color: transparent;
        border: none;
        padding-left: 0;
        font-weight: 500;
        color: #409eff;
      }

      .el-collapse-item__wrap {
        border: none;
        background-color: transparent;
      }

      .el-collapse-item__content {
        padding: 12px 0 0 0;
      }
    }
  }
}

// 空状态样式
.price-display:empty::before {
  content: "暂无数据";
  display: block;
  text-align: center;
  color: #c0c4cc;
  font-size: 14px;
  padding: 20px;
}
</style>
